package com.jingsheng.jingshengcity.mvp.ui.fragment.home;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.jingsheng.jingshengcity.R;
import com.jingsheng.jingshengcity.app.utils.GlideLoopImageLoader;
import com.jingsheng.jingshengcity.di.component.DaggerHomePageFragmentComponent;
import com.jingsheng.jingshengcity.di.module.HomePageFragmentModule;
import com.jingsheng.jingshengcity.mvp.contract.HomePageFragmentContract;
import com.jingsheng.jingshengcity.mvp.presenter.HomePageFragmentPresenter;
import com.jingsheng.jingshengcity.mvp.ui.adapter.HomePageFragmentAdapter;
import com.jingsheng.jingshengcity.mvp.ui.base.ViewPagerFragment;
import com.jingsheng.jingshengcity.widgets.decoration.GridItemDecoration;
import com.youth.banner.Banner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * Created by Administrator on 2017/5/5.
 */

public class HomePageFragment extends ViewPagerFragment<HomePageFragmentPresenter> implements HomePageFragmentContract.View, SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {

    @NonNull
    @BindView(R.id.swipe_layout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    @NonNull
    @BindView(R.id.rv_list)
    RecyclerView mRecyclerView;

    private Banner mBanner;
    private View mHeadView;
    private List<String> mList = new ArrayList<>();

    public static HomePageFragment newInstance() {
        HomePageFragment fragment = new HomePageFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerHomePageFragmentComponent
                .builder()
                .appComponent(appComponent)
                .homePageFragmentModule(new HomePageFragmentModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container) {
        return inflater.inflate(R.layout.fragment_home_page, container, false);
    }

    @Override
    public void initData() {
    }

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        UiUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (!b) {
            b = true;
            initView();
            initAdapter();
        } else {
            if (mBanner != null) {
                mBanner.startAutoPlay();
            }
        }
    }

    private void initAdapter() {

        mList.add("1");
        mList.add("2");
        mList.add("3");

        HomePageFragmentAdapter mHomePageFragmentAdapter = new HomePageFragmentAdapter(R.layout.adapter_home_page_item, mList);
        mHomePageFragmentAdapter.openLoadAnimation();
        mRecyclerView.addItemDecoration(new GridItemDecoration(getActivity(), R.drawable.list_divider_5));
        mRecyclerView.setAdapter(mHomePageFragmentAdapter);
        mHomePageFragmentAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mHomePageFragmentAdapter.addHeaderView(mHeadView);
    }

    private void initView() {
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
        mHeadView = getActivity().getLayoutInflater().inflate(R.layout.layout_home_page_head, null);
        mHeadView.setLayoutParams(new DrawerLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mBanner = (Banner) mHeadView.findViewById(R.id.banner);
        mBanner.setImageLoader(new GlideLoopImageLoader());
    }

    @Override
    protected void onInvisible() {
        if (mBanner != null) {
            mBanner.stopAutoPlay();
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public void onLoadMoreRequested() {

    }
}